package com.key.service.impl;

import com.key.common.R;
import com.key.common.TranException;
import com.key.common.vo.AssetFieldVO;
import com.key.entity.AssetCode;
import com.key.entity.AssetFieldLeft;
import com.key.entity.AssetFieldRight;
import com.key.mapper.AssetCodeMapper;
import com.key.service.AssetCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Service
public class AssetCodeServiceImpl implements AssetCodeService {
    private Lock lock = new ReentrantLock();

    @Autowired
    private AssetCodeMapper assetCodeMapper;

    @Override
    public R findAllAssetField() {
        R r=new R();
        List<AssetFieldLeft> assetFieldLefts = assetCodeMapper.selectAssetFieldLeft();
        List<AssetFieldRight> assetFieldRights = assetCodeMapper.selectAssetFieldRight();
        List<String> list = assetCodeMapper.selectAllAssetFieldRight();
        //System.out.println(list.toString());
        String prefix="";
        for (String s : list) {
            prefix+=s;
        }
        AssetCode assetCode = assetCodeMapper.selectAssetCode(prefix);
        if ((assetFieldLefts.size()+assetFieldRights.size())>0){
            AssetFieldVO assetFieldVO=new AssetFieldVO(assetCode.getNumcode(),assetFieldLefts,assetFieldRights);
            r=R.ok();
            r.setData(assetFieldVO);
        }else {
            r=R.no();
        }
        return r;
    }

    @Override
    @Transactional
    public R updateAssetCode(AssetFieldVO assetFieldVO) {
        R r=new R();
        try {
            int i = assetCodeMapper.deleteAssetFieldLeft();
            int i1 = assetCodeMapper.deleteAssetFieldRight();

            List<AssetFieldLeft> assetFieldLefts = assetFieldVO.getAssetFieldLefts();
            List<AssetFieldRight> assetFieldRights = assetFieldVO.getAssetFieldRights();
            for (AssetFieldLeft assetFieldLeft : assetFieldLefts) {
                assetCodeMapper.insertAssetFieldLeft(assetFieldLeft);
            }
            for (AssetFieldRight assetFieldRight : assetFieldRights) {
                assetCodeMapper.insertAssetFieldRight(assetFieldRight);
            }
            Integer numCode = assetFieldVO.getNumCode();
            List<String> strings = assetCodeMapper.selectAllAssetFieldRight();
            String prefix="";
            for (String s : strings) {
                prefix+=s;
            }
            AssetCode assetCode=new AssetCode();
            assetCode.setPrefix(prefix);
            assetCode.setNumcode(numCode);
            int result = assetCodeMapper.insertAssetCode(assetCode);
            r=R.ok();
        }catch (Exception e){
            throw new TranException("事务异常");
        }
        return r;
    }

    @Override
    @Transactional
    public String getCode() {
        try {
            lock.lock();
            List<String> strings = assetCodeMapper.selectAllAssetFieldRight();
            String prefix="";
            for (String s : strings) {
                prefix+=s;
            }
            int result = assetCodeMapper.updateAssetCode(prefix);
            AssetCode assetCode1 = assetCodeMapper.selectAssetCode(prefix);
            if(assetCode1 != null && result > 0 ){
                //判断【字符前缀】不为空 && 添加时记录数大于0  【进行String.format 拼接】
                return String.format("%s%"+assetCode1.getNumcode()+"d", assetCode1.getPrefix(),assetCode1.getMaxinx()).replace(" ", "0");
            }else{
                throw new TranException("获取编码失败");
            }
        } finally {
            lock.unlock();
        }

    }

}
